package java_class_work.week04;

public class ExceptionDemo01 {
    public static void main(String[] args) {
        /**
         * 1. 数组引用超界异常 - ArrayIndexOutOfBoundsException
         */
        int[] a = {1, 2, 3};
//        System.exit(0);
        try {
            System.out.println(a[3]);
            //建议写具体异常参数类型，不知道写Exception
            return;
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println(e);
        } catch (Exception e) {
            System.out.println(e);
        }finally {
            //即使前面执行了return，也会被执行，无条件执行。但是执行了exit()不会被执行。
            System.out.println("无条件执行finally");
        }
        /**
         * 2. 空指针异常 - NullPointerException
         */
        Demo01 d = null;
        try {
            System.out.println(d.toString());
        } catch (Exception e) {
            System.out.println("异常处理代码 ====== ");
            e.printStackTrace();
            System.out.println("=====================");
        }
        /**
         * 3. 算术异常 - ArithmeticException
         */
        try {
            System.out.println(4 / 0);
        } catch (Exception e) {
            System.out.println(e);
        }
        /**
         * 4. 数字格式化异常 - NumberFormatException
         */
        try{
            int b = Integer.parseInt("ab123");
        }catch (NumberFormatException e){
            e.printStackTrace();
        }
        /**
         *  5. 类型转换异常 - ClassCastException
         */
        try {
            Exception exception = new ClassCastException();
            NullPointerException exception1 = (NullPointerException)exception;
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
